package com.gdut.simple.view.activity.user;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.common.QRCodeUtils;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.view.basic.AbsBasicActivity;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;

public class QRCodeActivity extends AbsBasicActivity {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_user_qr_code)
    ImageView mUserQrCode;
    @BindView(R.id.id_user_profile)
    ImageView mUserProfile;
    @BindView(R.id.id_user_name)
    TextView mUserName;
    @BindView(R.id.id_user_area)
    TextView mUserArea;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_qr_code);
        ButterKnife.bind(this);
        initToolbar();
    }

    private void initToolbar() {
        mToolbar.setTitle("我的二维码");
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void loadData() {
        if (getIntent().getExtras() != null) {
            User user = getIntent().getExtras().getParcelable("user");
            if (user != null) {
                if (user.getProfile() != null) {
                    Picasso.with(this)
                            .load(user.getProfile().getUrl())
                            .error(R.drawable.ic_load_error)
                            .into(mUserProfile);
                } else {
                    Picasso.with(this)
                            .load(R.drawable.ic_load_error)
                            .into(mUserProfile);
                }
                if (user.getGender().equals("男")) {
                    mUserName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_male, 0);
                } else {
                    mUserName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_female, 0);
                }
                mUserName.setText(user.getNickName());
                mUserArea.setText(user.getArea());
            }
            String data = getIntent().getExtras().getString("data");
            mUserQrCode.setImageBitmap(QRCodeUtils.createQRCode(data));
        }
    }

    public static void startActivity(Activity activity, String data) {
        Intent intent = new Intent(activity, QRCodeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        // bundle.putParcelable("user", user);
        activity.startActivity(intent);
    }

    public static void startActivity(Activity activity, User user, String data) {
        Intent intent = new Intent(activity, QRCodeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        bundle.putParcelable("user", user);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

}
